package com.sihai.ehr.web.action.exam;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.biz.ExamPeriod;
import com.sihai.ehr.model.biz.ExamStatistics;
import com.sihai.ehr.model.sys.ExamPageEnum;
import com.sihai.ehr.model.sys.ExaminePeriodEnum;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.*;
import com.sihai.ehr.utils.PeriodUtils;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.pojo.ExamStatisticsItemPojo;
import com.sihai.ehr.web.pojo.ExamStatisticsPojo;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

@Results({
        @Result(name = "main", location = "exam_statistics_view_main.jsp"),
        @Result(name = "list", location = "exam_statistics_view_list.jsp"),
        @Result(name = "view", location = "exam_statistics_annual_view.jsp"),
        @Result(name="item_detail",location="exam_statistics_detail.jsp")
})
public class ExamStatisticsHistoryViewAction extends EHRAction {

    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private ExamAppManageService examAppManageService;
    @BeanInject
    private EmployeeService employeeService;
    @BeanInject
    private ExamPeriodService examPeriodService;
    private List<Organization> organizations;

    LimitedList<ExamStatisticsPojo> examStatisticsPojos;
    private Long empId;   //员工ID
    private Long dutyId;//职务ID
    private EmployeeinfoWork employeeinfoWork;

    /**
     * 页面编号
     */
    private String p;

    /**
     * 功能模块
     */
    private String module;

    /**
     * 考核周期类型
     */
    private ExaminePeriodEnum type;

    /**
     * 周期编号
     */
    private Long periodId;
    private List<ExamPeriod> examPeriods;//考核周期编号集合
    private ExamPeriod examPeriod;

    private List<ExamStatisticsItemPojo> examStatisticsItemPojos;//考核项目集合

    @Action("/exam/statisticsview_main")
    @Override
    public String execute() throws Exception {
        listAction = "/exam/statisticsview_list.xhtml";
        listTitle = "考核历史浏览";
        module = ExamPageEnum.valueOf(p).getBundle();
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        showChildOrg = true;
        return "main";
    }

    @Action("/exam/statisticsview_list")
    public String list() throws Exception {
        Calendar mCalendar = Calendar.getInstance();
        examPeriods = PeriodUtils.getLateThreeExamPeriodTOReverse(type,
                mCalendar.get(Calendar.YEAR),
                mCalendar.get(Calendar.MONTH) + 1);
        if (examPeriods != null) {
            examStatisticsPojos = examAppManageService
                    .searchExamStatisticsPojos(organizationId, dutyId,
                            type, examPeriods, createLimit(),
                            createSorter());
            /* 分页 */
            this.paging(examStatisticsPojos);
            /* 设置翻页action */
            this.setPageUrl("exam/statisticsview_list.xhtml");
        }

        return "list";
    }

    @Action("/exam/statisticsannualview")
    public String view() throws Exception {
        employeeinfoWork = employeeService.getEmployeeinfoWork(empId);
        int year = Integer.parseInt(getParameter("y"));
		int month = Integer.parseInt(getParameter("m"));
		periodId = examPeriodService.getExamPeriodByTypeAndYMonthly(type, year,
				month);
		examPeriod = examPeriodService.getExamPeriod(periodId);
        if (examPeriod == null) {
			examPeriod = new ExamPeriod();
			examPeriod.setExamYear(year);
			examPeriod.setExamMonthly(month);
			if (type == ExaminePeriodEnum.monthly) {
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.monthly.getPeriodFormat(), year,
						month));
			} else if (type == ExaminePeriodEnum.bimonthly) {
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.bimonthly.getPeriodFormat(), year,
						month - 1, month));
			} else if (type == ExaminePeriodEnum.season) {
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.season.getPeriodFormat(), year,
						month / 3));
			} else if (type == ExaminePeriodEnum.semiyearly) {
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.semiyearly.getPeriodFormat(), year,
						month / 6 > 1 ? "下" : "上"));
			}else{
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.annually.getPeriodFormat(), year));
			}
		}
        return "view";
    }

    @Action("/exam/statistics_item_detail")
    public String itemDetails(){
        int year = Integer.parseInt(getParameter("y"));
        int month = Integer.parseInt(getParameter("m"));
        periodId = examPeriodService.getExamPeriodByTypeAndYMonthly(type, year, month);
        examPeriod = examPeriodService.getExamPeriod(periodId); //考核周期编号
		if (examPeriod == null) {
			examPeriod = new ExamPeriod();
			examPeriod.setExamYear(year);
			examPeriod.setExamMonthly(month);
			if (type == ExaminePeriodEnum.monthly) {
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.monthly.getPeriodFormat(), year,
						month));
			} else if (type == ExaminePeriodEnum.bimonthly) {
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.bimonthly.getPeriodFormat(), year,
						month - 1, month));
			} else if (type == ExaminePeriodEnum.season) {
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.season.getPeriodFormat(), year,
						month / 3));
			} else if (type == ExaminePeriodEnum.semiyearly) {
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.semiyearly.getPeriodFormat(), year,
						month / 6 > 1 ? "下" : "上"));
			}else{
				examPeriod.setPeriodName(MessageFormat.format(
						ExaminePeriodEnum.annually.getPeriodFormat(), year));
			}
		}
          //根据考核周期编号和workID得到总分数
        examStatisticsItemPojos = examAppManageService.findExamstatisticsItems(examPeriod, empId, null, null);
         return "item_detail";
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(List<Organization> organizations) {
        this.organizations = organizations;
    }

    public LimitedList<ExamStatisticsPojo> getExamStatisticsPojos() {
        return examStatisticsPojos;
    }

    public void setExamStatisticsPojos(LimitedList<ExamStatisticsPojo> examStatisticsPojos) {
        this.examStatisticsPojos = examStatisticsPojos;
    }

    public Long getEmpId() {
        return empId;
    }

    public void setEmpId(Long empId) {
        this.empId = empId;
    }

    public Long getDutyId() {
        return dutyId;
    }

    public void setDutyId(Long dutyId) {
        this.dutyId = dutyId;
    }

    public EmployeeinfoWork getEmployeeinfoWork() {
        return employeeinfoWork;
    }

    public void setEmployeeinfoWork(EmployeeinfoWork employeeinfoWork) {
        this.employeeinfoWork = employeeinfoWork;
    }

    public String getP() {
        return p;
    }

    public void setP(String p) {
        this.p = p;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public ExaminePeriodEnum getType() {
        return type;
    }

    public void setType(ExaminePeriodEnum type) {
        this.type = type;
    }

    public List<ExamPeriod> getExamPeriods() {
        return examPeriods;
    }

    public void setExamPeriods(List<ExamPeriod> examPeriods) {
        this.examPeriods = examPeriods;
    }

    public Long getPeriodId() {
        return periodId;
    }

    public void setPeriodId(Long periodId) {
        this.periodId = periodId;
    }

    public ExamPeriod getExamPeriod() {
        return examPeriod;
    }

    public void setExamPeriod(ExamPeriod examPeriod) {
        this.examPeriod = examPeriod;
    }

    public List<ExamStatisticsItemPojo> getExamStatisticsItemPojos() {
        return examStatisticsItemPojos;
    }

    public void setExamStatisticsItemPojos(List<ExamStatisticsItemPojo> examStatisticsItemPojos) {
        this.examStatisticsItemPojos = examStatisticsItemPojos;
    }

}
